// Example 2.1: Explorer 16, Peripherial Libraries

// Device-Specific Configuration-bit settings
// Configuration Bits
#pragma config POSCMOD  = XT       // Primary Oscillator
#pragma config FNOSC    = PRIPLL	// Oscillator Selection
#pragma config FPLLIDIV = DIV_2    // PLL Input Divider
#pragma config FPLLMUL  = MUL_20   // PLL Multiplier
#pragma config FPLLODIV = DIV_1    // PLL Output Divider
#pragma config FPBDIV   = DIV_1    // Peripheral Clock divisor
#pragma config FWDTEN   = OFF      // Watchdog Timer
#pragma config WDTPS    = PS1      // Watchdog Timer Postscale
#pragma config FCKSM    = CSDCMD   // Clock Switching & Fail Safe Monitor
#pragma config OSCIOFNC = ON       // CLKO Enable
#pragma config IESO     = OFF      // Internal/External Switch-over
#pragma config FSOSCEN  = OFF      // Secondary Oscillator Enable
#pragma config CP       = OFF      // Code Protect
#pragma config BWP      = OFF      // Boot Flash Write Protect
#pragma config PWP      = OFF      // Program Flash Write Protect
#pragma config ICESEL   = ICS_PGx2 // ICE/ICD Comm Channel Select
#pragma config DEBUG    = ON       // Debugger Disabled for Starter Kit

#include <p32xxxx.h>
#include <plib.h>

int main(int argc, char** argv)
{
	// **********************************************************************
	// Configure MCU
	// **********************************************************************
	mJTAGPortEnable(DEBUG_JTAGPORT_OFF); // Disable JTAG

	PORTSetPinsDigitalOut(IOPORT_A, 0xFFFF);  // Set PORTA to Digital Out
	while (true)
	{
		PORTSetBits(IOPORT_A, 0xFFFF); // Write HIGH to PORTA
		WriteCoreTimer(0); // Reset CORE Timer
		while (ReadCoreTimer() < 20E6) // Wait for 40E6 clocks (0.5s)
		{
			Nop();
		}
		PORTClearBits(IOPORT_A, 0xFFFF); // Write LOW to PORTA
		WriteCoreTimer(0); // Reset CORE Timer
		while (ReadCoreTimer() < 20E6) // Wait for 40E6 clocks (0.5s)
		{
			Nop();
		}
	}
}
